﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Data;
using System.Windows.Markup;
using static AnChao.WPF.Controls.TextBox.Controls.CommandEmun;

namespace AnChao.WPF.Controls.TextBox.Controls
{
    public class ConverterTextToEditValue : MarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (value == null)
                {
                    return value;
                }

                switch (MaskType)
                {
                    case MaskType.Integer:
                        int intTemp = 0;
                        int.TryParse(value.ToString(), out intTemp);
                        return intTemp;
                    case MaskType.Decimal:
                    case MaskType.SignDecimal:
                        decimal decimalTemp = 0;
                        decimal.TryParse(value.ToString(), out decimalTemp);
                        return decimalTemp;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {

            }

            return value;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new ConverterTextToEditValue();
        }

        /// <summary>
        /// 转化类型
        /// </summary>
        public MaskType MaskType
        {
            get;
            set;
        }
    }
}
